package com.rabbit.eap.framework.config.jackson.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @projectName eap
 * @packageName com.rabbit.eap.framework.config.jackson.deserializer
 * @className JacksonDateDeserializer
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/4/27 20:54
 * <p>Description: JacksonDateDeserializer
 * Jackson Date序列化器
 * </p>

 */
public class JacksonDateDeserializer extends JsonDeserializer<LocalDateTime> {
    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        if (StringUtils.isBlank(jsonParser.getText())) {
            return null;
        }
        LocalDate localDate = LocalDate.parse(jsonParser.getText(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return LocalDateTime.of(localDate, LocalTime.MIN);
    }

}
